<template>
  <view>
    <!-- 
    <view>广告位1....</view>
    <view>广告位1....</view>
    <view>广告位1....</view>
    <view>广告位1....</view> 
    -->
    <!-- 搜索框 -->
    <view class="search-box">
      <my-search
        bgcolor="#007acc"
        radius="10rpx"
        title="搜你喜欢的~"
        @click="goToSearch"
      />
    </view>
    <!-- 1. 轮播图 -->
    <swiper
      class="top-swiper"
      indicator-dots
      autoplay
      circular
      :interval="3000"
      :duration="1000"
    >
      <!-- 轮播图中的每一项 -->
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <!-- 跳转到商品详情 -->
        <navigator
          :url="`/pages_sub1/product-detail/product-detail?pid=${item.goods_id}`"
          class="swiper-content"
        >
          <!-- <p>{{item.image_src}}</p> -->
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 2. 分类导航区域 -->
    <view class="nav-list">
      <view
        class="nav-item"
        v-for="(item, index) in navList"
        :key="index"
        @click="goToCategory(item)"
      >
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>
    <!-- 3. 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧 1 个大图 -->
          <view class="left-img-box">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
              @click="goToProductList(item.product_list[0])"
            ></image>
          </view>

          <!-- 右侧 4 个小图 -->
          <view class="right-img-box">
            <block v-for="(product, idx) in item.product_list" :key="idx">
              <view class="right-img-item" v-if="idx > 0">
                <image
                  :src="product.image_src"
                  :style="{ width: product.image_width + 'rpx' }"
                  mode="widthFix"
                  @click="goToProductList(product)"
                ></image>
              </view>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import badge from '@/mixins/badge'
export default {
  mixins: [badge],
  data() {
    return {
      // 1. 轮播图数据
      swiperList: [],
      // 2. 页内导航
      navList: [],
      // 3. 楼层数据数组
      floorList: [],
    }
  },
  created() {
    this.getSwiperList()
    this.getNavList()
    this.getFloorList()
  },
  methods: {
    // 1. 请求轮播图数据
    async getSwiperList() {
      // 1.1 发起请求
      const { meta, message } = await this.$http.get('/home/swiperdata')
      // 1.2 错误处理
      if (meta.status !== 200) {
        // uni.showToast({
        //   title: '请求轮播图数据失败',
        //   duration: 2000,
        //   icon: 'none'
        // })
        return this.$msg('请求轮播图数据失败')
      }
      // 1.3 赋值
      // this.$msg('请求轮播图数据成功')
      this.swiperList = message
    },
    // 2. 获取页内导航数据
    async getNavList() {
      const { meta, message } = await this.$http.get('/home/catitems')
      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg('获取首页导航数据失败')
      }
      this.navList = message
      // console.log(this.navList)
    },
    // 3. 跳转到分类页
    goToCategory(nav) {
      console.log(nav)
      // 判断是否是分类
      if (nav.name === '分类') {
        // 这里跳转只能用switchTab
        uni.switchTab({
          url: '/pages/category/category',
        })
      }
    },
    // 4. 获取楼层的数据
    async getFloorList() {
      const { meta, message } = await this.$http.get('/home/floordata')
      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg('获取首页楼层数据失败')
      }
      this.floorList = message
    },
    // 5. 跳转到商品列表页(传递参数:关键字)
    goToProductList(product) {
      // console.log(product)
      // 5.1 取出传递的参数
      const query = product.navigator_url.split('?')[1]
      // console.log(query)
      // 5.2 跳转到商品列表页
      uni.navigateTo({
        url: `/pages_sub1/product-list/product-list?${query}`,
      })
    },
    // 6. 跳转到搜索页面
    goToSearch() {
      // console.log('首页---跳转到搜索页面')
      uni.navigateTo({
        url: '/pages_sub1/search/search',
      })
    },
  },
}
</script>

<style lang="scss">
.top-swiper {
  height: 330rpx;

  .swiper-content,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 30rpx 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  display: flex;
  width: 100%;
  height: 60rpx;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}

.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
</style>
